//
//  WXDanMuCell.m
//  弹幕
//
//  Created by tianlong on 2018/12/27.
//  Copyright © 2018年 tianlong. All rights reserved.
//

#import "WXDanMuCell.h"
#import "Masonry.h"
#import "WXDanMuHeader.h"
#import "UIView+WXAutoLayout.h"

#define RandomColor [UIColor colorWithRed:(arc4random_uniform(256) / 255.0) green:(arc4random_uniform(256) / 255.0) blue:(arc4random_uniform(256) / 255.0) alpha:1.0]

@interface WXDanMuCell ()
@property (weak,   nonatomic) UIView *backgroundView;
@end

@implementation WXDanMuCell

- (instancetype)initWithDanMuMessage:(WXDanMuMessage *)message identifier:(NSString *)identifier {
    self = [super init];
    if (self) {
        self.identifier = identifier;
        self.msg = message;
        self.backgroundColor = RandomColor;
        
        UIView *bgView = [[UIView alloc] init];
        bgView.backgroundColor = [UIColor blackColor];
        bgView.alpha = 0.3;
        [self addSubview:bgView];
        self.backgroundView = bgView;
        
        UILabel *headLabel = [[UILabel alloc] init];
        headLabel.font = [UIFont fontWithName:WXDanMuCellTitleLabelFontName size:self.AutoFitFontSize(WXDanMuCellHeadLabelFontSize)];
        headLabel.font = [UIFont fontPingFangSCWithSize:self.AutoFitFontSize(WXDanMuCellTitleLabelFontSize)];
        headLabel.lineBreakMode = WXDanMuCellLabelLineBreakMode;
        headLabel.textColor = [UIColor whiteColor];
        headLabel.textAlignment = NSTextAlignmentCenter;
        headLabel.text = @"战队";
        headLabel.backgroundColor = [UIColor colorWithHexValue:0xFF9E00];
        [self addSubview:headLabel];
        self.headLabel = headLabel;
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [self titleFont];
        titleLabel.lineBreakMode = WXDanMuCellLabelLineBreakMode;
        titleLabel.textColor = [UIColor whiteColor];
        [self addSubview:titleLabel];
        self.titleLabel = titleLabel;
        
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = RandomColor;
    }
    return self;
}

- (void)setMsg:(WXDanMuMessage *)msg {
    _msg = msg;
    self.titleLabel.text = msg.content.length > 0 ? msg.content : @"";
    self.headLabel.hidden = !msg.isMyTeam;
}

- (UIFont *)titleFont {
    UIFont *font = [UIFont fontWithName:WXDanMuCellTitleLabelFontName size:self.AutoFitFontSize(WXDanMuCellTitleLabelFontSize)];
    font = [UIFont fontPingFangSCWithSize:self.AutoFitFontSize(WXDanMuCellTitleLabelFontSize)];
    return font;
}

- (CGSize)getTextSize:(NSString *)text font:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode maxSize:(CGSize)maxSize {
    CGSize size;
    UILabel *label = [[UILabel alloc] init];
    label.font = font;
    label.text = text;
    label.numberOfLines = 0;
    label.lineBreakMode = lineBreakMode;
    [[UIApplication sharedApplication].delegate.window addSubview:label];
    
    [label sizeToFit];
    [[UIApplication sharedApplication].delegate.window layoutIfNeeded];
    size = label.size;
    if (size.width > maxSize.width) {
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.offset(0);
            make.width.offset(maxSize.width);
        }];
        [[UIApplication sharedApplication].delegate.window layoutIfNeeded];
        size = label.size;
    }
    
    [label removeFromSuperview];
    
    return size;
}

- (CGSize)getAttributedTextSize:(NSAttributedString *)attributedText maxSize:(CGSize)maxSize {
    CGSize size;
    UILabel *label = [[UILabel alloc] init];
    label.numberOfLines = 0;
    label.attributedText = attributedText;
    [[UIApplication sharedApplication].delegate.window addSubview:label];
    
    [label sizeToFit];
    [[UIApplication sharedApplication].delegate.window layoutIfNeeded];
    size = label.size;
    if (size.width > maxSize.width) {
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.offset(0);
            make.width.offset(maxSize.width);
        }];
        [[UIApplication sharedApplication].delegate.window layoutIfNeeded];
        size = label.size;
    }
    
    [label removeFromSuperview];
    return size;
}

#pragma mark - 更新布局
- (void)updateLayout {
    CGFloat titleTopM       = self.AutoFitTop(WXDanMuCellTitleLabelTopM);
    CGFloat titleLeftM      = self.msg.isMyTeam ? self.AutoFitLeft(WXDanMuCellHasHeadLableLeftM) : WXDanMuCellTitleLableLeftM;
    CGFloat titleH          = self.AutoFitHeight(WXDanMuCellTitleLabelHeight);
    if (self.msg && self.msg.isMyTeam) {
        CGFloat headW       = self.AutoFitWidth(48);
        CGFloat headH       = self.AutoFitHeight(20);
        CGFloat headTopM    = self.AutoFitTop(5);
        CGFloat headLeftM   = self.AutoFitLeft(5);
        [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top    .offset(titleTopM);
            make.left   .offset(titleLeftM);
            make.right  .offset(-WXDanMuCellTitleLableLeftM);
            make.height .offset(titleH);
        }];
        
        [self.headLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top    .offset(headTopM);
            make.left   .offset(headLeftM);
            make.width  .offset(headW);
            make.height .offset(headH);
        }];
        self.headLabel.layer.cornerRadius = headH*0.5;
        self.headLabel.layer.masksToBounds = YES;
    }
    else {
        [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top    .offset(titleTopM);
            make.left   .offset(titleLeftM);
            make.right  .offset(-WXDanMuCellTitleLableLeftM);
            make.height .offset(titleH);
        }];
    }
    
    [self.backgroundView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.offset(0);
    }];
}

@end
